// pages/tender/info/info.js
const { imgUrl } = getApp().globalData;
import { tenderApi } from "@/utils/request/api/tender";
import { publicApi } from "@/utils/request/api/public";
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    imgUrl: imgUrl + "tender/",
    current: 0,
    search: "",
    selectsCurrent: 0,
    showAlert: false,
    ptList: [
      { isOpen: true },
      { isOpen: false },
      { isOpen: true },
      { isOpen: true },
      { isOpen: true },
      { isOpen: true },
      { isOpen: true },
      { isOpen: true },
      { isOpen: true },
      { isOpen: true },
    ],

    refresherStatus: false,
    hasMore: true,
    page: 1,
    page_size: 10,
    total: 0,
    list: [],
    industryList: [],
    areaList: [],

    myQuery: {},
  },

  attached() {
    this.getList();
  },
  created() {
    publicApi.getIndustryList().then((res) => {
      let arr = res.data.map((item, index) => ({
        name: item.name,
        id: index + 1,
      }));
      arr.unshift({
        name: "不限",
        id: 0,
      });
      this.setData(
        {
          industryList: arr,
        },
        () => {
          wx.setStorageSync("myIndustry", arr);
        }
      );
    });
    publicApi.getAreaList().then((res) => {
      console.log("res area === ", res);
      let arr = res.rows
        .filter((item) => item.level == 1)
        .map((item) => ({
          name: item.name,
          id: item.code,
        }));
      arr.unshift({
        name: "不限",
        id: 0,
      });
      this.setData(
        {
          areaList: arr,
        },
        () => {
          wx.setStorageSync("myArea", arr);
        }
      );
    });
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onChange(event) {
      this.setData(
        {
          page: 1,
          hasMore: true,
          list: [],
          total: 0,
          myQuery: {},
          search: "",
          current: event.currentTarget.dataset.current,
        },
        () => {
          this.getList();
        }
      );
    },
    onInput(event) {
      this.setData({
        search: event.detail.value,
      });
    },
    onBlur() {
      this.getList();
    },
    tapSelects(event) {
      this.setData(
        {
          selectsCurrent: event.currentTarget.dataset.current,
        },
        () => {
          this.setData({
            showAlert: true,
          });
        }
      );
    },

    tapConfirm(event) {
      this.setData(
        {
          myQuery: event.detail,
          showAlert: false,
        },
        () => {
          this.getList();
        }
      );
    },
    toGgDetail(event) {
      const id = event.currentTarget.dataset.id;
      wx.navigateTo({
        url: "/pages/tender/info/gbDetail/gbDetail?id=" + id,
      });
    },
    toQyDetail(event) {
      const id = event.currentTarget.dataset.id;
      wx.navigateTo({
        url: "/pages/tender/info/qyDetail/qyDetail?id=" + id,
      });
    },
    openItem(event) {
      let index = event.currentTarget.dataset.index;
      let list = JSON.parse(JSON.stringify(this.data.ptList));
      list[index].isOpen = !list[index].isOpen;
      this.setData({
        ptList: list,
      });
    },
    // 上下拉加载
    // 触底
    scrolltolower() {
      if (!this.data.hasMore) {
        return;
      }
      let page = this.data.page + 1;
      this.setData({
        page,
      });
      this.getList();
    },
    // 下拉刷新
    handleRefresherRefresh() {
      this.setData({
        myQuery: {},
        search: "",
        refresherStatus: true,
        page: 1,
        hasMore: true,
      });
      this.getList();
    },
    getList() {
      if (this.data.current == 0) {
        let query = {
          page: this.data.page,
          page_size: this.data.page_size,
        };
        Object.assign(query, this.data.myQuery);
        if (this.data.search.trim()) {
          query.title = this.data.search;
        }
        tenderApi.getAnnouncementList(query).then((res) => {
          let arr = res.data.list;
          this.setData({
            list: this.data.page == 1 ? arr : this.data.list.concat(arr),
            total: res.data.total,
            refresherStatus: false,
            hasMore: this.data.page * this.data.page_size <= res.data.total,
          });
        });
      } else if (this.data.current == 1) {
        let query = {
          page: this.data.page,
          page_size: this.data.page_size,
        };
        let obj = JSON.parse(JSON.stringify(this.data.myQuery));
        obj.set_up_date_start = obj.start_create_date;
        obj.set_up_date_end = obj.end_create_date;
        Object.assign(query, this.data.myQuery);
        if (this.data.search.trim()) {
          query.keyword = this.data.search;
        }
        tenderApi.getCompanyList(query).then((res) => {
          let arr = res.data.list;
          this.setData({
            list: this.data.page == 1 ? arr : this.data.list.concat(arr),
            total: res.data.total,
            refresherStatus: false,
            hasMore: this.data.page * this.data.page_size <= res.data.total,
          });
        });
      }
    },
  },
});
